﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.Model.BaseModel;
using HR60_PanelSoft.View.Base;
using HR60_PanelSoft.View.TabForms.Handles;


namespace HR60_PanelSoft.View.TabForms.ListItems
{
    //GC: [g1]修正不保存到内存 [SceneDeviceList]@s1
    //GC: [g2]修正内存对象不加载 [SceneDeviceList]@s1

	/// <summary>
	/// 单控设备场景控制列表
	/// 主要提供修改与删除SceneEvent两个事件
	/// </summary>
	public partial class SceneDeviceList : BaseFormControl
	{
		public SceneDeviceList()
		{
			InitializeComponent();
		}

		private Scene scene = null;

		/// <summary>
		/// 场景中单控设备事件改变
		/// </summary>
		public event DeviceEventChangeHandle DeviceEventChange;

		// 目标场景
		public Scene TagScene
		{
			get
			{
				return scene;
			}
		}

		// 初始化界面
		public void init(Scene scene)
		{
			this.scene = scene;
			selGroup.bindData((IList)deviceGroups, "name");


            /* g2.s2 */

            /**/
            
            if (scene.events.Count > 0 && selGroup.Value != null )
            {
                refreshDeviceList((DeviceGroup)selGroup.Value);
            }


		}

		// 刷新单控设备列表
		private void refreshDeviceList(DeviceGroup group)
		{
			CatchValueChange = false;

			IList<Device> devs = group.findButtonCtrlDevices();
			tblList.Controls.Clear();
			tblList.RowCount = devs.Count;
			foreach (Device dev in devs)
			{
				SceneDeviceItem item = new SceneDeviceItem(dev);
				SceneEvent se = scene.findEventByDevice(dev.uuid);
                /* g2.s1:加载内存对象 */
                if (null != se)
                    item.LoadValue(se);
                /* end of g2.s1 */
                
				item.Value = se;
				item.AutoSubmitFlag = false;
				tblList.Controls.Add(item);

				//item.ValueChanged += new EventHandler(item_ValueChanged);
                item.ValueChanged += new ValueChangedHandler(item_ValueChanged);
                /* g1.s2 打开自动提交开关  */
                item.AutoSubmitFlag = true;
                /* end of g1.s2 */
			}

            

			CatchValueChange = true;
		}

		void item_ValueChanged(object sender, EventArgs e)
		{
			SceneDeviceItem item = ((SceneDeviceItem)sender);

            /* g1.s1 保存事件到场景内存对象 */
            //SceneEvent se = (SceneEvent)item.Value;

            /* [g1.s3] 保存到内存对象 */
            //item.SaveValue(this.scene);
            /* [g1.s4] 优化保存，将保存动作重新，放在此处 */
            item.SaveValue(this.scene);
            /* end of [g1.s4] */
            //string s = null;
            //s = proj.scenes[0].events.Count.ToString();
            //Scene s = proj.scenes.Find(x => x.uuid == this.scene.uuid);
            //if (s == null)
            //    return;
            //item.SaveValue(s);

            /* end of [g1.s3] */

			// 场景事件发生改变
			//if (DeviceEventChange != null) DeviceEventChange(item.TagDevice, se);

            /* end of g1.s1 */

		}

		// 设备分组选择事件
		private void selGroup_ValueChanged(object sender, EventArgs e)
		{
			if (selGroup.Value != null) 
				refreshDeviceList((DeviceGroup)selGroup.Value);
		}

		// 全开
		private void btnOpen_Click(object sender, EventArgs e)
		{
			foreach (Control control in tblList.Controls)
			{
				SceneDeviceItem item = (SceneDeviceItem)control;
				item.open();
			}
		}

		// 全关
		private void btnClose_Click(object sender, EventArgs e)
		{
			foreach (Control control in tblList.Controls)
			{
				SceneDeviceItem item = (SceneDeviceItem)control;
				item.close();
			}
		}



	}
}
